//
//  MyShopViewController.m
//  ShouYi
//
//  Created by yurui on 16/3/11.
//  Copyright © 2016年 yurui. All rights reserved.
//

#import "MyShopViewController.h"
#import "HeaderView.h"
#import "MyShopCollectionViewCell.h"
@interface MyShopViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) UIImageView *heardIgView;
@property(nonatomic,strong) UIImageView *shopIconIg;
@property(nonatomic,strong) UILabel *shopName;
@property(nonatomic,strong) UIButton *collectBt;
@property(nonatomic,assign) BOOL isCollect;

@end

@implementation MyShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.title = @"我的小店";
    self.view.backgroundColor = [UIColor whiteColor];
    [self configerCollectonView];
}

- (void)configerCollectonView{
    UICollectionViewFlowLayout *layout=[[ UICollectionViewFlowLayout alloc ] init ];
    layout.itemSize = CGSizeMake((kWidth - 40) / 2 , 210);
    layout.sectionInset = UIEdgeInsetsMake(10, 15, 0, 15);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = kViewBackColor;
    [self.view addSubview:_collectionView];

    [_collectionView registerClass:[MyShopCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    //注册页眉
    [_collectionView registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

}
#pragma mark UICollectionViewDataSource 和 UICollectionViewDelgeate代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 12;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyShopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.titleLabel.text = @"你在南方的艳阳里大雪纷飞，我在北方的寒夜里四季如春。";
    cell.imageView.image = [UIImage imageNamed:@"img_touxxxxx.png"];
    cell.priceLabel.text = [NSString stringWithFormat:@"￥%@",@"125.0"];
    return cell;
}
//设置分区的页眉（区头）返回对应的视图对象，需要重用以及自定义页眉视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    //如果是为页眉设置自定义视图
    if (kind == UICollectionElementKindSectionHeader) {
        HeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        //为页眉上的控件赋
        headerView.backgroundColor = kViewBackColor;
        self.heardIgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 210)];
        self.heardIgView.image = [UIImage imageNamed:@"fj.jpg"];
        [headerView addSubview:_heardIgView];
        UIView *nameView = [[UIView alloc]initWithFrame:CGRectMake(0, 170, kWidth, 40)];
        nameView.backgroundColor = [UIColor whiteColor];
        [headerView addSubview:nameView];
        self.shopName = [[UILabel alloc]initWithFrame:CGRectMake(80, 0, kWidth - 150, 40)];
        self.shopName.text = self.title;
        self.shopName.adjustsFontSizeToFitWidth = YES;
        [nameView addSubview:_shopName];
        
        self.collectBt = [UIButton buttonWithType:UIButtonTypeCustom];
        _collectBt.frame = CGRectMake(kWidth - 60, 5, 30, 30);
        _collectBt.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
        NSString *collect = [kUserDefault objectForKey:@"collect"];
        self.isCollect = [collect integerValue];
        if (self.isCollect == 0) {
            [_collectBt setImage:[UIImage imageNamed:@"iconfont-shoucang_1.png"] forState:UIControlStateNormal];
        }else{
            [_collectBt setImage:[UIImage imageNamed:@"iconfont-shoucang.png"] forState:UIControlStateNormal];
        }
        [_collectBt addTarget:self action:@selector(collectShop) forControlEvents:UIControlEventTouchUpInside];
        [nameView addSubview:_collectBt];
        
        
        self.shopIconIg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 140, 60, 60)];
        self.shopIconIg.image = [UIImage imageNamed:@"img_touxxxxx.png"];
        [headerView addSubview:_shopIconIg];
        
        
        
        return headerView;
    }else{
        return nil;
    }
}
//headerView 高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (collectionView == self.collectionView) {
        
        if (section == 0) {
            
            return CGSizeMake(kWidth,210);
        }else {
            return CGSizeMake(kWidth, 0);
        }
    }
    
    return CGSizeMake(0,0);
}

// 点击收藏图标响应事件
- (void)collectShop{
    if (_isCollect == 0) {
        [_collectBt setImage:[UIImage imageNamed:@"iconfont-shoucang.png"] forState:UIControlStateNormal];
        _isCollect = 1;
        NSLog(@"收藏");
    }else{
        [_collectBt setImage:[UIImage imageNamed:@"iconfont-shoucang_1.png"] forState:UIControlStateNormal];
        _isCollect = 0;
        NSLog(@"取消收藏");
    }
    [kUserDefault setObject:[NSString stringWithFormat:@"%d",_isCollect] forKey:@"collect"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
